/**
 * Copyright(C) com.allinpay - 老纳慢走
 * 
 * @akonziz 陆某人
 * 
 * @since 2013年10月31日
 * @description: <描述>
 * 
 */

var stop = false;
var request_complete = true;
var restlist = new Array();
var ajax_data = {"vendorid" : v_id, "orderby" : "id", "startindex" : 0, "access_token":access_token};

$(document).ready(function() {
	$(window).scroll(function() {
		if (!stop) {
			checkload(-1, scroll_loading);
		}
	});
	
	scroll_loading();
});

// 页面滚动增量加载
function scroll_loading() {
	if (request_complete == true){
		request_complete = false;
		ajax_post_json("apis/group/list", ajax_data, m_beforeSend, rt_success, m_error, rt_complete);
	}
}

function m_beforeSend(xhr){
	showLoadingDiv();
};

function rt_complete(event,xhr,options){
	request_complete = true;
	hideLoadingDiv();
	AdsPicture_request();
};

function m_error(XMLHttpRequest, textStatus, errorThrown) {
	request_complete = true;
}
/*
 <div class="list-block cf">
	  <img src="${logoImgUrl}/${item.logoPath}" style="width: 70px; height: 70px; float: left;" />
	  <a href="javascript:goUrl('<c:url value='/restaurant/index?t=g-s&rid=${item.id}'/>')">
      <div class="bl-m c-river-bg">
           <p class="bl-tit">${item.name}</p>
           <p class="bl-sub-tit">${item.comments}</p>
           <p class="bl-price">人均：￥${item.averagePrice}元</p>
           <p class="bl-sub-tit">地址:${item.area.parent.parent.name}${item.area.parent.name}${item.area.name}${item.address}元</p>
      </div>
      </a>
 </div> 
 */
function rt_success(data) {
	if (data.result == true) {
		if (data.type == "data") {
			var div_rest_list = $("#restaurant_list");
			$.each(data.content, function(index, item) {
				
				var nodeHtml = "<div class=\"list-block cf\">";
				nodeHtml += "<img src=\""+imgPath+"/" + item.logoPath + "\" style=\"width: 70px; height: 70px; float: left;\" />";
				nodeHtml += "<a id='a"+item.id+"' onclick=\"javascript:userChoose('"+item.id+"', '"+$("#r_link").val()+item.id+"')\">";
				nodeHtml += "<div class=\"bl-m c-river-bg height-auto\" style=\"width: 216px;\">";
				nodeHtml += "   <p class=\"bl-tit\">" + item.name + "</p>";
				nodeHtml += "   <p class=\"bl-sub-tit\">电话：" + item.tel + "</p>";
				nodeHtml += "   <p class=\"bl-sub-tit\">地址：" + item.address + "</p>";
				nodeHtml += "</div></a></div> ";
				
				div_rest_list.append(nodeHtml);
				ajax_data.startindex++;;
			});
		} else if (data.type = "msg") {
			stop = true;  //没有更多的数据，不要重复请求；
		}
	} else if (data.result == false) {
		if (data.type = "msg") {
		}
	}
};

function userChoose(id, url) {
	$("#a"+id).parent(".list-block").toggleClass("list-block-active");
	setTimeout(function() {
		$("#a"+id).parent(".list-block").toggleClass("list-block-active");
		goUrl(url);
	},100);
};

// Carousel
var adsPicArr = new Array();
function newAdsPicture(id, pic){
	var r_map = new Object();
	r_map.id = id;
	r_map.pic = pic;
	return r_map;
}
function AdsPicture_request(){
	if (adsPicArr != null && adsPicArr.length < 1){
		ajax_post_json("apis/group/ads", {"vendorid": v_id, "access_token":access_token}, ads_beforeSend, AdsPicture_Success, ads_error, ads_complete);
	}
}
function ads_beforeSend(xhr){
	showLoadingDiv();
};
function ads_error(XMLHttpRequest, textStatus, errorThrown) {
}
function ads_complete(event,xhr,options){
	hideLoadingDiv();
};
function AdsPicture_Success(data) {
	var n = 0;
	if (data.type=="data"){
		$.each(data.content,function(index,item){
			adsPicArr[n++] = newAdsPicture(index, item);
		});
		if (adsPicArr.length > 0) {
			$("#default_recommend_pic").css({display:"none"});
		}
		buildCarousel();
		$("#r_carousel").css({display:""});
	}else if (data.type="msg"){
		//$("#default_recommend_pic").css({display:"none"});
	}

	$('.carousel').carousel({
		  interval: 2000
	});
}
/*
<div class="item active">
	<img src="..." alt="...">
	<div class="carousel-caption"></div>
</div>
 */
function buildCarousel() {
	$("#r_inner").empty();
	$("#r_indicators").empty();
	
	$.each(adsPicArr ,function(index,item){
		// carousel-indicators
		var liItem = $("<li data-target='#r_carousel' data-slide-to=" + index + ">");
		if (index == 0) liItem.addClass("active");
		else liItem.addClass("");
		$("#r_indicators").append(liItem);
		
		// carousel-inner
		var divItem = $("<div>");
		if (index == 0) divItem.addClass("item active");
		else divItem.addClass("item");
		$("#r_inner").append(divItem);
		
		var imgItem = $("<img style='width:320px; height: 130px; border: 0;'>");
		imgItem.attr("src",  adsImgPath+"/" + item.pic);
		divItem.append(imgItem);
	});
}